package com.hup.windowUtil.view.customView;

import com.hup.utils.commons.TextUtil;
import com.hup.utils.swing.customView.VerticalCheckBox;
import com.hup.utils.swing.customView.ViewText;
import com.hup.windowUtil.model.WindowData;
import lombok.extern.log4j.Log4j2;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author hugan
 * @date 2021/9/1
 */
@Log4j2
public class WindowItem extends JPanel {
    public static final String CMD_ON_TOP = "置顶";
    public static final String CMD_RESIZE = "大小";
    public static final String CMD_MAX = "最大化";

    public JPanel root;
    public ViewText txtTitle;
    public VerticalCheckBox cbOnTop;
    public JButton btnResize;
    public VerticalCheckBox cbResize;
    public VerticalCheckBox cbMaximize;
    private WindowData windowData;

    public WindowItem(SettingActionListener listener) {
        $$$setupUI$$$();

        ActionListener checkboxListener = e -> {
            if (windowData == null) return;
            JCheckBox checkBox = (JCheckBox) e.getSource();
            boolean selected = checkBox.isSelected();

            Runnable failureCallback = () -> checkBox.setSelected(!selected);
            listener.checkBoxAction(e.getActionCommand(), selected, windowData, failureCallback);
        };

        cbOnTop.setActionCommand(CMD_ON_TOP);
        cbResize.setActionCommand(CMD_RESIZE);
        cbMaximize.setActionCommand(CMD_MAX);
        cbOnTop.addActionListener(checkboxListener);
        cbResize.addActionListener(checkboxListener);
        cbMaximize.addActionListener(checkboxListener);

        btnResize.addActionListener(e -> {
            if (windowData == null) return;
            listener.showResize(windowData);
        });
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        root.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        txtTitle = new ViewText();
        txtTitle.setLineWrap(true);
        txtTitle.setText("标题");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        root.add(txtTitle, gbc);
        cbOnTop = new VerticalCheckBox();
        cbOnTop.setText("置顶");
        cbOnTop.setVerticalTextPosition(1);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5);
        root.add(cbOnTop, gbc);
        btnResize = new JButton();
        btnResize.setText("大小");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        root.add(btnResize, gbc);
        cbResize = new VerticalCheckBox();
        cbResize.setText("可调大小");
        cbResize.setVerticalTextPosition(1);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5);
        root.add(cbResize, gbc);
        cbMaximize = new VerticalCheckBox();
        cbMaximize.setText("可最大化");
        cbMaximize.setVerticalTextPosition(1);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5);
        root.add(cbMaximize, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

    public void initData(WindowData windowData) {
        this.windowData = windowData;
        if (!TextUtil.isBlank(windowData.processPath)) {
            String file = new File(windowData.processPath).getName();
            txtTitle.setText(file + ": " + windowData.title);

            txtTitle.setToolTipText(windowData.processPath);
        } else {
            txtTitle.setText(windowData.title);
            txtTitle.setToolTipText("获取失败,可能是权限不足");
        }
        cbOnTop.setSelected(windowData.isOnTop);
        cbResize.setSelected(windowData.canResize);
        cbMaximize.setSelected(windowData.canMax);
    }

    public interface SettingActionListener {
        void checkBoxAction(String cmd, boolean selected, WindowData windowData, Runnable failureCallback);

        void showResize(WindowData windowData);
    }

}
